package com.fkg.test.RxjavaRxtrofit;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.fkg.test.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

public class RxjavaRxtrofitActivity extends AppCompatActivity {

    @BindView(R.id.btn_affirm)
    Button btn_affirm;
    @BindView(R.id.tv_tip)
    TextView tv_tip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rxjava_retrofit);
        ButterKnife.bind(RxjavaRxtrofitActivity.this);
    }

    @OnClick(R.id.btn_affirm)
    public void onClick(){
        getMovie();
    }

    private void getMovie(){
        Subscriber subscriber = new Subscriber<MovieEntity>() {
            @Override
            public void onCompleted() {
                Toast.makeText(RxjavaRxtrofitActivity.this, "Get Top Movie Completed", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Throwable e) {
                tv_tip.setText(e.getMessage());
            }

            @Override
            public void onNext(MovieEntity movieEntity) {
                tv_tip.setText(movieEntity.toString());
            }
        };
        HttpMethods.getInstance().getTopMovie((org.reactivestreams.Subscriber<MovieEntity>) subscriber, 0, 10);
    }

    private void getRxjavaRetrofit(){
        /*String baseUrl = "https://api.douban.com/v2/movie/";

        Retrofit retrof = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                //我们定义的service返回值就不在是一个Call了，而是一个Observable
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        MovieService movieService = retrof.create(MovieService.class);
        movieService.getTopMovieOb(0, 10)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<MovieEntity>() {
                    @Override
                    public void onCompleted() {
                        Toast.makeText(RxjavaRxtrofitActivity.this, "Get Top Movie Completed", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Throwable e) {
                        tv_tip.setText(e.getMessage().toString()+"");
                    }

                    @Override
                    public void onNext(MovieEntity movieEntity) {
                        tv_tip.setText(movieEntity.toString()+"");
                    }
                });*/
    }

    /**
     * 纯retrofit
     */
    private void getRetrofit(){

        /*String baseUrl = "https://api.douban.com/v2/movie/";

        Retrofit retrof = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        MovieService movieService = retrof.create(MovieService.class);

        Call<MovieEntity> call = movieService.getTopMovieCall(0,10);

        call.enqueue(new Callback<MovieEntity>() {
            @Override
            public void onResponse(Call<MovieEntity> call, Response<MovieEntity> response) {
                tv_tip.setText(response.body().toString());
            }

            @Override
            public void onFailure(Call<MovieEntity> call, Throwable t) {
                tv_tip.setText(t.getMessage());
            }
        });*/

    }
}
